package com.atguigu.servlet;

import com.atguigu.constant.OrderStatus;
import com.atguigu.domain.Order;
import com.atguigu.service.OrderService;
import com.atguigu.service.impl.OrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author dl
 * @date 2020/5/4 22:17
 */
@WebServlet(name = "ManagerOrderServlet", urlPatterns = "/admin/order")
public class ManagerOrderServlet extends BaseServlet {

    private OrderService orderService = new OrderServiceImpl();

    /**
     * manager find all orders
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void allOrders(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Order> list = orderService.listOrders();
        req.setAttribute("allOrders",list);
        req.getRequestDispatcher("/pages/manager/order_manager.jsp").forward(req,resp);
    }

    /**
     * manager update order status
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void updateOrderStatus(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String orderId = req.getParameter("order_id");
        orderService.update(orderId, OrderStatus.DELIVERED.toString());
        String referer = req.getHeader("Referer");
        resp.sendRedirect(referer);
    }


}
